package our.pack;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class TrackActivity extends Activity{

	private TracksDataSource trackDatasource;
	private PointsDataSource pointsDatasource;
	private ListView listview;
	private ArrayAdapter<Track> adapter;
	private TrackAdapter trackAdapter;
	private ArrayList<Track> tracks;
	public static TrackActivity s_trackActivity;
	private static final int result = 999;

	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		s_trackActivity = this;


		setContentView(R.layout.track);

		trackDatasource = new TracksDataSource(this);
		trackDatasource.open();
		pointsDatasource = new PointsDataSource(this);
		pointsDatasource.open();

		listview = (ListView)findViewById(R.id.tracklist);
		registerForContextMenu(listview);
		LayoutInflater inflater = LayoutInflater.from(this);

		tracks = (ArrayList<Track>) trackDatasource.getAllTracks();
		trackAdapter = new TrackAdapter(TrackActivity.this, R.layout.trackview, tracks);
		listview.setAdapter(trackAdapter);

		listview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				registerForContextMenu(listview);
				view.showContextMenu();
			}
		});

           
		for( Track track : trackDatasource.getAllTracks())
		{
			Log.d("tag", "track = " + track.toString() + " id = " + track.getID());
		}
		
      }

	/**
	 * 
	 * @param trackName De naam van de track die je verwijderd wil hebben
	 * 
	 */
	public void deleteTrack(String trackName)
	{
		for(Track track : trackDatasource.getAllTracks())
		{
			if(track.getTrackName().equals(trackName)){
				trackDatasource.deleteTrack(track);
			}
		}
	}



	/**
	 * verwijderd alle tracks uit de database
	 */
	public void removeAllTracks(){
		for(Track track : trackDatasource.getAllTracks())
		{
			trackDatasource.deleteTrack(track);
		}
	}

	/**
	 * update de listview door een nieuwe trackadapter aan te maken
	 */
	public void update(){
		ArrayList<Track> t = (ArrayList<Track>) trackDatasource.getAllTracks();
		if(tracks.size() != t.size() ){
			trackAdapter = new TrackAdapter(TrackActivity.this, R.layout.trackview, t);
			tracks = t;
			listview.setAdapter(trackAdapter);
			trackAdapter.notifyDataSetChanged();
			Log.d("trackactivity", "hier ook");
		}


	}

	/**
	 * creates a context menu
	 */
	 @Override
    public void onCreateContextMenu(android.view.ContextMenu menu, View v, android.view.ContextMenu.ContextMenuInfo menuInfo) 
    {
    	super.onCreateContextMenu(menu, v, menuInfo);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.trackcontextmenu, menu);
    	
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    	Track track = (Track) trackAdapter.getItem(info.position);
    	
    	
    	menu.setHeaderTitle(track.getTrackName());
    }
	
	 /**
	  * handler voor de items van het contextMenu
	  */
	 @Override
    public boolean onContextItemSelected(MenuItem item) 
    {
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    	
    	int id = item.getItemId();
    	int pos = info.position;
    	
    	if(id == R.id.itemrename )
    	{
    		trackDatasource.open();
    		pointsDatasource.open();
    		
    		Track track = trackAdapter.getItem(pos);
    		Intent i = new Intent(TrackActivity.this, RenameTrackActivity.class);
    		i.putExtra("Track", track);
    		
    		startActivityForResult(i, result);
    	}
    	else if(id == R.id.itemremove)
    	{
    		showDialog(pos);
    		
    	}
    	else if(id == R.id.itemsimulate)
    	{
    		Track track = trackAdapter.getItem(pos);
    		Intent i = getParent().getIntent();
    		i.putExtra("Track", track);
    		
    		TabActivity ta = (TabActivity) TrackActivity.this.getParent();
    	    ta.getTabHost().setCurrentTab(0);

    	}
    	
    	return super.onContextItemSelected(item);
    }
	 
	 /**
	  * Dialog om de gebruiker te laten weten dat er een track verwijdert gaat worden.
	  */
	 @Override
	    protected Dialog onCreateDialog(int id) {
	    
	    	final Track track = trackAdapter.getItem(id);
	    	AlertDialog.Builder builder = new AlertDialog.Builder(TrackActivity.this);
	    	builder.setMessage("Wil je " + track.getTrackName() + " echt verwijderen?" ).setCancelable(false).setPositiveButton("Ja", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
		    		
					trackDatasource.deleteTrack(track);
					ArrayList<Track> t = (ArrayList<Track>) trackDatasource.getAllTracks();
					trackAdapter = new TrackAdapter(TrackActivity.this, R.layout.trackview, t);
					tracks = t;
					listview.setAdapter(trackAdapter);
					
				}
			})
			.setNegativeButton("Nee", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
					
				}
			});
	    	
	    	AlertDialog alert = builder.create();
	    	
	    	return alert;
	    	
	    	
	    }
	 
	@Override
	protected void onResume() {
	super.onResume();
	update();
	}

	/**
	 * Wordt aangeroepen als de RenameTrackActivity stopt
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		ArrayList<Track> t = (ArrayList<Track>) trackDatasource.getAllTracks();
		trackAdapter = new TrackAdapter(TrackActivity.this, R.layout.trackview, t);
		tracks = t;
		listview.setAdapter(trackAdapter);
	}


}
